<?php
//
//	file: includes/log/plug_advlog_modcp.php
//	begin: 30/04/2007
//	$Author: christopher $
//	$Revision: 31 $
//	$Date: 2008-07-21 03:08:30 +0000 (Mon, 21 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: plug for advlog to modcp

if (!defined('IN_PHPBB'))
{
	die("Hacking attempt");
}

class class_advlog_modcp
{
	// @public action for editing/deleting while in viewtopic or posting
	function validate()
	{
		global $user, $mode, $post_info, $forum_id, $topic_id, $post_id;

		// user is original poster, so do not record
		// TO-DO: add a more complex check to see if there has been a post following this one
		// also, add a config point to allow this to be switched on
		if($post_info['poster_id'] == $user->data['user_id'])
		{
			return;
		}

		// template
		$tpl_data = array(
			'TOPIC_TITLE' => $log_data['topic_title'],
			'U_TOPIC' => external_url('viewtopic', array(POST_TOPIC_URL => $topic_id)),
			'U_POST' => external_url('viewtopic', array( POST_POST_URL => $post_id)),
		);

		// db log
		$log_data = array();
		switch ($mode)
		{
			case 'editpost':
				$type = 'editpost';
				$log_data += array(
					'topic_id' => $topic_id,
					'post_id' => $post_id,
				);
				break;
			case 'delete':
				if ($post_info['topic_first_post_id'] == $post_info['topic_last_post_id'])
				{
					$type = 'delete';
					$log_data += array(
						'topics' => array(
							$db->sql_escape_string($post_info['topic_title']),
						),
					);
					$tpl_data['TOPICS'] = $post_info['topic_title'];
				}
				else
				{
					$type = 'deletepost';
					$log_data += array(
						'topic_id' => $topic_id,
					);
				}
				break;
		}

		// cleanup
		$this->add_action($type, $forum_id, $log_data);
		$this->notify($tpl_data);
		unset($log_data, $tpl_data, $type);
		return;
	}

	// @public action for deleting while in modcp
	function delete_topic()
	{
		global $db, $forum_id, $topic_id_sql;

		// db log
		// get topic titles for storage since the topic will be gone afterwards
		$topics = array();
		$var = array('key' => 'topic_id', 'value_in' => $topic_id_sql, 'include_key' => 'forum_id', 'include_value' => $forum_id, 'extra' => array('topic_title'));
		if($result = get_table_by_field(TOPICS_TABLE, $var, __FILE__, __LINE__))
		{
			while ($row = $db->sql_fetchrow($result))
			{
				$topics[] = $db->sql_escape_string($row['topic_title']);
			}
			$db->sql_freeresult($result);
		}

		// template
		$tpl_data = array(
			'TOPICS' => implode("\n", $log_data['topics']),
		);

		// cleanup
		$this->add_action('delete', array('topics' => $topics));
		$this->notify('delete', $forum_id, $tpl_data);
		unset($topics, $tpl_data, $var);
		return;
	}

	// @public action for moving a topic
	function move_topic()
	{
		global $db, $forums, $forum_id, $new_forum_id, $topic_list;

		// template
		$var = array('key' => 'topic_id', 'value_in' => $topic_list, 'extra' => array('topic_title'));
		if($result = get_table_by_field(TOPICS_TABLE, $var, __FILE__, __LINE__))
		{
			while ($row = $db->sql_fetchrow($result))
			{
				$topics[] = $row['topic_title'] . ' <' . external_url('viewtopic', array(POST_TOPIC_URL => $row['topic_id'])) . '>';
			}
		}
		$db->sql_freeresult($result);

		$tpl_data = array(
			'U_FORUM_NEW' => external_url('index', array(POST_FORUM_URL => $new_forum_id)),
			'FORUM_NAME_NEW' => $forums->data[$new_forum_id]['forum_name'],
			'TOPICS' => implode("\n", $topics),
		);

		// cleanup
		$this->add_action('move', $forum_id, array('topics' => explode(', ', $topic_list), 'new_forum' => $new_forum_id);
		$this->notify($tpl_data);
		unset($topics, $tpl_data, $var);
		return;
	}

	// @public action for (un)lockng a topic
	function lock_unlock_topic()
	{
		global $db, $user, $mode, $forum_id, $topic_id_sql;

		// template
		$var = array('key' => 'topic_id', 'value_in' => $topic_id_sql, 'extra' => array('topic_title'));
		if($result = get_table_by_field(TOPICS_TABLE, $var, __FILE__, __LINE__))
		{
			while ($row = $db->sql_fetchrow($result))
			{
				$topics[] = $row['topic_title'] . ' <' . external_url('viewtopic', array(POST_TOPIC_URL => $row['topic_id']) . '>';
			}
		}
		$db->sql_freeresult($result);

		$tpl_data = array(
			'MOD_TYPE' => $user->lang('log_' . $mode . 'ed'),
			'TOPICS' => implode("\n", $topics),
		);

		// cleanup
		$this->add_action($mode, $forum_id, array('topics' => explode(', ', $topic_id_sql));
		$this->notify($tpl_data);
		unset($topics, $tpl_data, $var);
		return;
	}

	// @public action for splitting a topic
	function split_topic()
	{
		global $db, $forums, $topic_id, $forum_id, $new_topic_id, $new_forum_id, $post_id_sql, $post_subject;

		$posts = $posts_url = array();

		// db log
		$var = array('key' => 'post_id', 'value_in' => $post_id_sql);
		if($result = get_table_by_field(POSTS_TABLE, $var, __FILE__, __LINE__))
		{
			$posts = $db->sql_fetchrowset($result);
		}
		$db->sql_freeresult($result);

		// template
		$var = array('key' => 'topic_id', 'value' => $topic_id, 'start' => 0, 'limit' => 1, 'extra' => array('topic_title'));
		if($result = get_table_by_field(TOPICS_TABLE, $var, __FILE__, __LINE__))
		{
			$row = $db->sql_fetchrow($result);
			$old_topic_title = $row['topic_title'];
		}
		$db->sql_freeresult($result);

		foreach ( $posts as $post_id )
		{
			$posts_url[] = $row['post_id'] . ' <' . external_url('viewtopic', array(POST_POST_URL => $row['post_id']) . '>';
		}
		$tpl_data = array(
			'U_TOPIC_NEW' => external_url('viewtopic', array(POST_TOPIC_URL => $new_topic_id)),
			'U_TOPIC' => external_url('viewtopic', array(POST_TOPIC_URL => $topic_id)),
			'TOPIC_TITLE_NEW' => $post_subject,
			'TOPIC_TITLE' => $old_topic_title,
			'U_FORUM_NEW' => external_url('index', array(POST_FORUM_URL => $new_forum_id)),
			'FORUM_NAME_NEW' => $forums->data[$new_forum_id]['forum_name'],
			'POSTS' => implode("\n", $posts_url),
		);

		// cleanup
		$this->add_action('split', $forum_id, array('new_topic_id' => $new_topic_id, 'old_topic_id' => $topic_id, 'new_forum_id' => $new_forum_id, 'posts' => $posts));
		$this->notify($tpl_data);
		unset($posts, $tpl_data, $var, $posts_url, $old_topic_title);
		return;
	}

	// @private common method to insert data into db
	function add_action($type, $location, $log_data)
	{
		global $db, $config, $user;

		$this->type = $type;

		$fields = array(
			'log_type' => $type,
			'log_time' => time(),
			'log_user_id' => $user->data['user_id'],
			'log_user_ip' => $user->session->ip,
			'log_location' => $location,
			'log_data' => serialize($log_data),
		);
		$db->sql_stack_statement($fields);
		$db->sql_stack_insert(LOGS_TABLE, false, __LINE__, __FILE__);
		unset($log_data, $fields);

		return;
	}

	// @private common method to send email notifications
	function notify($tpl_data)
	{
		global $config, $user, $forums, $forum_id;

		if(!$config->data['advlog_notify'])
		{
			return;
		}

		$mod_id = $user->data['user_id'];
		$mod_name = $user->data['username'];

		$forum_name = $forums->data[$forum_id]['forum_name'];

		$tpl = 'advlog_' . ($this->type == 'unlock') ? 'lock' : $this->type;
		$tpl_data += array(
			'U_FORUM' => external_url('index', array(POST_FORUM_URL => $forum_id)),
			'FORUM_NAME' => $forum_name,
			'MODERATOR' => $mod_name,
		);

		$auth_users = array();
		$auth_users = get_users_by_auth('auth_log_notify', $forum_id);

		// remove the culprit from the list
		if(isset($auth_users[$mod_id]))
		{
			unset($auth_users[$mod_id]);
		}

		// return if there are no users to notify
		if (empty($auth_users))
		{
			return;
		}

		// use aucp::nitofy if available
		if ($config->data['mod_aucp_notify'])
		{
			$notify = new advnot_parent();
			$notify->add_users($auth_users);
			$notify->prepare('logs_email_title', '');
			$notify->template($tpl, $tpl_data);
			$notify->send();
		}
		// else use generic emailer
		else
		{
			send_email($auth_users, $tpl, $tpl_data, 'logs_email_title', 'lang_extend_advlog');
		}
		return;
	}
}

?>